package org.wddsicmd.widgets;
 
import org.wddsicmd.app.ButtonTabComponent;
import org.wddsicmd.util.ResourceBundleUtil;
import org.wddsicmd.util.Log;


import java.lang.Object;

import java.io.*;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.*;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

import java.awt.*;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.Insets;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.Image;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.Timer;
import javax.swing.JTable;
import javax.swing.table.*;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.event.TreeModelListener;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.Document;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;

import javax.swing.Icon;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import javax.swing.plaf.basic.BasicGraphicsUtils;

public class CustomTabbedPaneUI extends BasicTabbedPaneUI {

/* doom-2015-10-23 14:31:10 
    private Color selectColor;
    private Color deSelectColor;
 doom-2015-10-23 14:31:10 */
    private int inclTab = 3;
    private int anchoFocoV = inclTab;
    private int anchoFocoH = 1;
    private int anchoCarpetas = 18;
    private Polygon shape;
/* doom-2015-10-23 14:31:23 
    private Color bgColor;
    private Color frontColor;
 doom-2015-10-23 14:31:23 */

    private ColorSet selectedColorSet;
    private ColorSet defaultColorSet;
    private ColorSet hoverColorSet;

    private int mTabWidth = 150;
    private int mParentWidth = 0;
    private int mTabCount = 0;
    private int[] mDisabledTabs;

    public CustomTabbedPaneUI(int parentWidth, int tabCount){

	 mTabCount = tabCount;
	 updateTabAreaInsets(parentWidth);
	 mDisabledTabs = new int[mTabCount];
/* doom-2015-10-23 14:31:27 
	 String bgColorHex = "#5b9bd5";
	 String frontColorHex = "#deebf7";
		
        setColor(bgColorHex,frontColorHex);
 doom-2015-10-23 14:31:27 */

        selectedColorSet = new ColorSet();
        selectedColorSet.topGradColor1 = Color.decode("#deebf7");//选中的最上层
        selectedColorSet.topGradColor2 = Color.decode("#deebf7");//选中的第二层
        selectedColorSet.bottomGradColor1 = Color.decode("#deebf7");//选中的第三层
        selectedColorSet.bottomGradColor2 = Color.decode("#deebf7");//选中的最下层
        defaultColorSet = new ColorSet();
        defaultColorSet.topGradColor1 = Color.decode("#5b9bd5");//未选的最上层
        defaultColorSet.topGradColor2 = Color.decode("#5b9bd5");
        defaultColorSet.bottomGradColor1 = Color.decode("#5b9bd5");
        defaultColorSet.bottomGradColor2 = Color.decode("#5b9bd5");
        hoverColorSet = new ColorSet();
        hoverColorSet.topGradColor1 = Color.decode("#5b9bd5");//鼠标悬停最上层
        hoverColorSet.topGradColor2 = Color.decode("#5b9bd5");
        hoverColorSet.bottomGradColor1 = Color.decode("#5b9bd5");
        hoverColorSet.bottomGradColor2 = Color.decode("#5b9bd5");

    }
//  
//  public static ComponentUI createUI(JComponent c) {
//      return new TabbedPaneUI();
//  }

    @Override
    protected void installDefaults() {
        super.installDefaults();
        RollOverListener l = new RollOverListener();
        tabPane.addMouseListener(l);
        tabPane.addMouseMotionListener(l);
/* doom-2015-10-23 14:31:8 
        selectColor = Color.decode("#deebf7");
        deSelectColor = Color.decode("#5b9bd5");
 doom-2015-10-23 14:31:8 */
/* doom-2015-10-23 15:8:3 
        tabAreaInsets.right = anchoCarpetas;
 doom-2015-10-23 15:8:3 */

/* doom-2015-10-23 15:7:33 */
/* doom-2015-10-23 15:7:52 
        if (tabInsets == null) tabInsets = new Insets(0,0,0,0);
        if (selectedTabPadInsets == null) selectedTabPadInsets = new Insets(0,0,0,0);
        if (tabAreaInsets == null) tabAreaInsets = new Insets(0,0,0,0);
        if (contentBorderInsets == null) contentBorderInsets = new Insets(0,0,0,0);
 doom-2015-10-23 15:7:52 */
/* doom-2015-10-23 15:7:33 */

/* doom-2015-10-23 18:11:29 */
/* doom-2015-10-27 10:44:0 
		tabAreaInsets = new Insets(0, mParentWidth - mTabWidth*3, 0, 0);
 doom-2015-10-27 10:44:0 */
		tabAreaInsets = new Insets(0, 0, 0, 0);
		selectedTabPadInsets = new Insets(0, 0, 0, 0);
		contentBorderInsets = new Insets(0, 0, 0, 0);
/* doom-2015-10-23 18:11:29 */

    }

    public void updateTabAreaInsets(int parentWidth){
/* doom-2015-10-27 10:43:57 
		tabAreaInsets = new Insets(0, parentWidth -  mTabWidth*3, 0, 0);
 doom-2015-10-27 10:43:57 */
	 mParentWidth = parentWidth;
	 mTabWidth = mParentWidth / mTabCount;
    }

    public void setDisabledTabs(int[] list){
      if(list != null && list.length == mDisabledTabs.length){
		for(int index = 0; index < list.length; index++){
			mDisabledTabs[index] = list[index];
		}
      }
    }

    public void setColor(String bgColorHex, String frontColorHex){
/* doom-2015-10-23 14:31:19 
        bgColor = Color.decode(bgColorHex);
        frontColor = Color.decode(frontColorHex);
 doom-2015-10-23 14:31:19 */
    }

    @Override
    protected void paintContentBorderTopEdge(Graphics g, int tabPlacement,
            int selectedIndex, int x, int y, int w, int h) {
/* doom-2015-10-23 18:7:10 
        super.paintContentBorderTopEdge(g, tabPlacement, -1, x, y, w, h);
 doom-2015-10-23 18:7:10 */
    }

    @Override
    protected void paintTabArea(Graphics g, int tabPlacement, int selectedIndex) {
            g.setColor(Color.decode("#5b9bd5"));
    
/* doom-2015-10-23 14:57:17 
        if (runCount > 1) {
            int lines[] = new int[runCount];
            for (int i = 0; i < runCount; i++) {
                lines[i] = rects[tabRuns[i]].y
                        + (tabPlacement == TOP ? maxTabHeight : 0);
            }
            Arrays.sort(lines);
            if (tabPlacement == TOP) {
                int fila = runCount;
                for (int i = 0; i < lines.length - 1; i++, fila--) {
                    Polygon carp = new Polygon();
                    carp.addPoint(0, lines[i]);
                    carp.addPoint(tabPane.getWidth() - 2 * fila - 2, lines[i]);
                    carp.addPoint(tabPane.getWidth() - 2 * fila, lines[i] + 3);
                    if (i < lines.length - 2) {
                        carp.addPoint(tabPane.getWidth() - 2 * fila,
                                lines[i + 1]);
                        carp.addPoint(0, lines[i + 1]);
                    } else {
                        carp.addPoint(tabPane.getWidth() - 2 * fila, lines[i]
                                + rects[selectedIndex].height);
                        carp.addPoint(0, lines[i] + rects[selectedIndex].height);
                    }
                    carp.addPoint(0, lines[i]);
                    g.setColor(hazAlfa(fila));
                    g.fillPolygon(carp);
                    g.setColor(darkShadow.darker());
                    g.drawPolygon(carp);
                }
            } else {
                int fila = 0;
                for (int i = 0; i < lines.length - 1; i++, fila++) {
                    Polygon carp = new Polygon();
                    carp.addPoint(0, lines[i]);
                    carp.addPoint(tabPane.getWidth() - 2 * fila - 1, lines[i]);
                    carp.addPoint(tabPane.getWidth() - 2 * fila - 1,
                            lines[i + 1] - 3);
                    carp.addPoint(tabPane.getWidth() - 2 * fila - 3,
                            lines[i + 1]);
                    carp.addPoint(0, lines[i + 1]);
                    carp.addPoint(0, lines[i]);
                    g.setColor(hazAlfa(fila + 2));
                    g.fillPolygon(carp);
                    g.setColor(darkShadow.darker());
                    g.drawPolygon(carp);
                }
            }
        }
 doom-2015-10-23 14:57:17 */
        super.paintTabArea(g, tabPlacement, selectedIndex);
    }

    @Override
    protected void paintTabBackground(Graphics g, int tabPlacement,
            int tabIndex, int x, int y, int w, int h, boolean isSelected) {
        Graphics2D g2D = (Graphics2D) g;
        ColorSet colorSet;

        Rectangle rect = rects[tabIndex];
        if (isSelected) {
            colorSet = selectedColorSet;
        } else if (getRolloverTab() == tabIndex) {
            colorSet = hoverColorSet;
        } else {
            colorSet = defaultColorSet;
        }
        g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        int width = rect.width;
        int xpos = rect.x;
        int yPos = rect.y;
/* doom-2015-10-23 14:38:31 
        if (tabIndex > -1) {
            width--;
            xpos++;
            yPos += 2;
        }
 doom-2015-10-23 14:38:31 */


/* doom-2015-10-23 15:8:29 
        g2D.setPaint(new GradientPaint(xpos, 0, colorSet.topGradColor1, xpos,
                h / 2, colorSet.topGradColor2));
        g2D.fill(this.getArea(xpos, yPos, width, rect.height));
 doom-2015-10-23 15:8:29 */


/* doom-2015-10-23 14:37:51 
        g2D.setPaint(new GradientPaint(0, h / 2, colorSet.bottomGradColor1, 0,
                h, colorSet.bottomGradColor2));
        g2D.fill(this.getDownArea(xpos, yPos, width, h - 2));
 doom-2015-10-23 14:37:51 */

    }

    private Shape getArea(int x, int y, int w, int h) {
        RoundRectangle2D rect = new RoundRectangle2D.Float(x, y, w, h, 15, 15);
        Area a = new Area(rect);
        Rectangle2D rec = new Rectangle2D.Float(x, y + h / 2, w, h / 2);
        Area b = new Area(rec);
        a.add(b);
        return a;
    }

    private Shape getUpArea(int x, int y, int w, int h) {
        Rectangle2D rec = new Rectangle2D.Float(x, y, w, h / 3 );
        Area a = new Area(rec);
        RoundRectangle2D rect = new RoundRectangle2D.Float(x, y, w, h, 15, 15);
        Area b = new Area(rect);
        a.intersect(b);
        return a;
    }

    private Shape getDownArea(int x, int y, int w, int h) {
        Rectangle2D rec = new Rectangle2D.Float(x, y + h / 3, w, h*2 / 3 +1);
        Area a = new Area(rec);
        RoundRectangle2D rect = new RoundRectangle2D.Float(x, y, w, h, 15, 15);
        // Area b = new Area(rect);
        // a.intersect(b);
        return a;
    }
    private class ColorSet {
        Color topGradColor1;
        Color topGradColor2;
        Color bottomGradColor1;
        Color bottomGradColor2;
    }

    @Override
    protected void paintText(Graphics g, int tabPlacement, Font font,
            FontMetrics metrics, int tabIndex, String title,
            Rectangle textRect, boolean isSelected) {
        super.paintText(g, tabPlacement, font, metrics, tabIndex, title,
                textRect, isSelected);
        g.setFont(font);
        View v = getTextViewForTab(tabIndex);
        if (v != null) {
            // html
            v.paint(g, textRect);
        } else {
            // plain text
            int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);
            if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
                g.setColor(tabPane.getForegroundAt(tabIndex));
                BasicGraphicsUtils
                        .drawStringUnderlineCharAt(g, title, mnemIndex,
                                textRect.x, textRect.y + metrics.getAscent());
            } else { // tab disabled
                g.setColor(Color.BLACK);
                BasicGraphicsUtils
                        .drawStringUnderlineCharAt(g, title, mnemIndex,
                                textRect.x, textRect.y + metrics.getAscent());
                g.setColor(tabPane.getBackgroundAt(tabIndex).darker());
                BasicGraphicsUtils.drawStringUnderlineCharAt(g, title,
                        mnemIndex, textRect.x - 1,
                        textRect.y + metrics.getAscent() - 1);
            }
        }
    }

    //固定tab标签宽度
    @Override
    protected int calculateTabWidth(int tabPlacement, int tabIndex,
            FontMetrics metrics) {
        return mTabWidth;
//      return 10 + anchoFocoV
//              + super.calculateTabWidth(tabPlacement, tabIndex, metrics);
    }

    @Override
    protected int calculateTabHeight(int tabPlacement, int tabIndex,
            int fontHeight) {
        if (tabPlacement == LEFT || tabPlacement == RIGHT) {
            return super.calculateTabHeight(tabPlacement, tabIndex, fontHeight);
        } else {
            return anchoFocoH
                    + super.calculateTabHeight(tabPlacement, tabIndex,
                            fontHeight);
        }
    }

    protected int calculateMaxTabHeight(int tabPlacement) {
        return 50;
    }

    //set tab title hight
    @Override
    protected void paintTabBorder(Graphics g, int tabPlacement, int tabIndex,
            int x, int y, int w, int h, boolean isSelected) {

		if(mDisabledTabs[tabIndex] == 1)
			return;
/* doom-2015-10-27 11:4:26 
	   if(tabIndex == 3 || tabIndex == 4)
	   	return;
 doom-2015-10-27 11:4:26 */
			
//      if (isSelected) {
	   if (isSelected) {
            g.setColor(Color.decode("#deebf7"));
		Graphics2D g2D = (Graphics2D) g;
		RoundRectangle2D roundedRectangle = new RoundRectangle2D.Float(x + 4, y + 4, w - 8, h - 8, 10, 10);		
		g2D.fillRoundRect(x + 4, y + 4, w - 8, h - 8, 10, 10);
            g.setColor(Color.decode("#41719c"));
		g2D.draw(roundedRectangle);
	   }else{
            g.setColor(Color.decode("#5b9bd5"));
		Graphics2D g2D = (Graphics2D) g;
		RoundRectangle2D roundedRectangle = new RoundRectangle2D.Float(x + 4, y + 4, w - 8, h - 8, 10, 10);		
		g2D.fillRoundRect(x + 4, y + 4, w - 8, h - 8, 10, 10);
            g.setColor(Color.decode("#41719c"));	
		g2D.draw(roundedRectangle);
	   }
/* doom-2015-10-23 14:54:0 
            int[] xp = new int[] { x, x, x + 6, x + w - inclTab - 12,
                    x + w - inclTab - 6, x + w - inclTab, x + w - inclTab, x };
            int[] yp = new int[] { y + h, y + 6, y, y, y, y + 6, y + h, y + h };
            shape = new Polygon(xp, yp, xp.length);
            Graphics2D g2D = (Graphics2D) g;
 doom-2015-10-23 14:54:0 */
/* doom-2015-10-23 14:44:54 
            g.setColor(Color.white);
            g.drawLine(x, y + h, x + w - inclTab, y + h);
 doom-2015-10-23 14:44:54 */
//      }
    }

    protected int getTabLabelShiftY(int tabPlacement, int tabIndex,
            boolean isSelected) {
        return 0;
    }

    protected int getTabLabelShiftX(int tabPlacement, int tabIndex,
            boolean isSelected) {
        return 0;
    }

    @Override
    protected void paintFocusIndicator(Graphics g, int tabPlacement,
            Rectangle[] rects, int tabIndex, Rectangle iconRect,
            Rectangle textRect, boolean isSelected) {
        if (tabPane.hasFocus() || isSelected) {
            // g.setColor(UIManager.getColor("ScrollBar.thumbShadow"));
            // g.drawPolygon(shape);
        }
    }

    protected Color hazAlfa(int fila) {
        int alfa = 0;
        if (fila >= 0) {
            alfa = 50 + (fila > 7 ? 70 : 10 * fila);
        }
        return new Color(0, 0, 0, alfa);
    }

    private int lastRollOverTab = -1;

    private class RollOverListener implements MouseMotionListener,
            MouseListener {
        public void mouseDragged(MouseEvent e) {
        }
        public void mouseMoved(MouseEvent e) {
            checkRollOver();
        }
        public void mouseClicked(MouseEvent e) {
        }
        public void mousePressed(MouseEvent e) {
        }
        public void mouseReleased(MouseEvent e) {
        }
        public void mouseEntered(MouseEvent e) {
            checkRollOver();
        }

        public void mouseExited(MouseEvent e) {
            tabPane.repaint();
        }

        private void checkRollOver() {
            int currentRollOver = getRolloverTab();
            if (currentRollOver != lastRollOverTab) {
                lastRollOverTab = currentRollOver;
                Rectangle tabsRect = new Rectangle(0, 0, tabPane.getWidth(), 
                        tabPane.getHeight()+1);
                tabPane.repaint(tabsRect);
            }
        }
    }
}
